using System;
using System.Web.Caching;
using ChannelVN.Cached.Config;
using System.Web;

namespace ChannelVN.Cached.CacheClients
{
    public class CacheDependency
    {
        private CacheSettings _CacheSettings
        {
            get
            {
                return CacheSettings.GetCurrentSettings();
            }
        }

        public void Add(string database, string tableName, string key, object value)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            Add(database, tableName, key, value, DateTime.Now.AddSeconds(_CacheSettings.DefaultCacheExpire));
        }
        public void Add(string database, string[] tableNames, string key, object value)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            Add(database, tableNames, key, value, DateTime.Now.AddSeconds(_CacheSettings.DefaultCacheExpire));
        }
        public void Add(string filePath, string key, object value)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            Add(filePath, key, value, DateTime.Now.AddSeconds(_CacheSettings.DefaultCacheExpire));
        }

        public void Add(string database, string tableName, string key, object value, uint numberOfSecondForExpires)
        {
            Add(database, tableName, key, value, DateTime.Now.AddSeconds(numberOfSecondForExpires));
        }
        public void Add(string database, string[] tableNames, string key, object value, uint numberOfSecondForExpires)
        {
            Add(database, tableNames, key, value, DateTime.Now.AddSeconds(numberOfSecondForExpires));
        }
        public void Add(string filePath, string key, object value, uint numberOfSecondForExpires)
        {
            Add(filePath, key, value, DateTime.Now.AddSeconds(numberOfSecondForExpires));
        }

        public void Add(string database, string tableName, string key, object value, DateTime expireDate)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            var sqlDep = new SqlCacheDependency(database, tableName);
            if (value != null)
                HttpContext.Current.Cache.Insert(key, value, sqlDep, expireDate, Cache.NoSlidingExpiration);
        }
        public void Add(string database, string[] tableNames, string key, object value, DateTime expireDate)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            var aggCacheDep = new AggregateCacheDependency();
            var sqlDepGroup = new SqlCacheDependency[tableNames.Length];
            for (int i = 0; i < tableNames.Length; i++)
            {
                sqlDepGroup[i] = new SqlCacheDependency(database, tableNames[i]);

            }
            aggCacheDep.Add(sqlDepGroup);
            HttpContext.Current.Cache.Insert(key, value, aggCacheDep, expireDate, Cache.NoSlidingExpiration);
        }
        public void Add(string filePath, string key, object value, DateTime expireDate)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            var fileDependency = new System.Web.Caching.CacheDependency(filePath);
            HttpContext.Current.Cache.Insert(key, value, fileDependency, expireDate, TimeSpan.Zero, CacheItemPriority.Normal, null);
        }

        public object Get(string key)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return null;
            return HttpContext.Current.Cache[key];
        }
        public T Get<T>(string key)
        {
            try
            {
                if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return default(T);
                return (T)HttpContext.Current.Cache[key];
            }
            catch
            {
                return default(T);
            }
        }

        public void Remove(string key)
        {
            if (null == _CacheSettings || !_CacheSettings.EnableCacheDependency) return;
            HttpContext.Current.Cache.Remove(key);
        }
    }
}
